package com.icngor;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class API extends HttpServlet {

	private static final long serialVersionUID = 1L;
//	public static void main(String[] args) {
//		String str = "01|SRV1A4G0000000001|01|3002|01|01|eyJ0cnhObyI6ImFhZGY2MWVjZTY4NjQyMjE5Y2M5ZDNkNzBhNDcyOGFmIiwibG9hbkluZm9zIjpbeyJib3Jyb3dUeXBlIjoxLCJib3Jyb3dTdGF0ZSI6MiwiYm9ycm93QW1vdW50IjozLCJjb250cmFjdERhdGUiOjEzNDM3NzkyMDAwMDAsImxvYW5QZXJpb2QiOjI0LCJyZXBheVN0YXRlIjo4LCJhcnJlYXJzQW1vdW50IjowLCJjb21wYW55Q29kZSI6IlAyMVQyQ0YxMTAzMzY0MjYyIn0seyJib3Jyb3dUeXBlIjoxLCJib3Jyb3dTdGF0ZSI6MiwiYm9ycm93QW1vdW50IjozLCJjb250cmFjdERhdGUiOjEzNDM3NzkyMDAwMDAsImxvYW5QZXJpb2QiOjI0LCJyZXBheVN0YXRlIjo5LCJhcnJlYXJzQW1vdW50IjowLCJjb21wYW55Q29kZSI6IlAyMVQyQ0YxMTAzMzY0MjMyIn0seyJib3Jyb3dUeXBlIjoxLCJib3Jyb3dTdGF0ZSI6MiwiYm9ycm93QW1vdW50IjozLCJjb250cmFjdERhdGUiOjEzNDM3NzkyMDAwMDAsImxvYW5QZXJpb2QiOjI0LCJyZXBheVN0YXRlIjo5LCJhcnJlYXJzQW1vdW50IjowLCJjb21wYW55Q29kZSI6IlAyMVQyQ0YxMTAzMzY0MjQxIn0seyJib3Jyb3dUeXBlIjoxLCJib3Jyb3dTdGF0ZSI6MiwiYm9ycm93QW1vdW50IjozLCJjb250cmFjdERh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6IlAyMVQyQ0YxMT";
//		String[] split = str.split("\\|");
//		for (String s : split) {
//			
//			System.out.println(s);
//		}
//	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setHeader("Access-Control-Allow-Origin","*");
		System.out.println("GET : "+request.getQueryString());
		
		ServletOutputStream os = response.getOutputStream();
//		doPost(request, response);
		os.write("true".getBytes());
		
		os.flush();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		
	
		File file = new File("/adot.log");
		System.out.println(file.getAbsolutePath());
		if (file.exists()) {
			writeFile(request,file);
		} else {
			if (file.createNewFile()) {
				writeFile(request,file);
			} else {

				System.out.println(file.getAbsolutePath());
			}

		}

	}

	public void writeFile(HttpServletRequest request ,File file) throws IOException {
		SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
		String str = "\n\nPOST Request at:"+sdf.format(new Date().getTime())+"\n\n";
		FileOutputStream fos = new FileOutputStream(file,true);
		fos.write(str.getBytes());
		fos.flush();
		
		
		Enumeration<String> headerNames = request.getHeaderNames();
		
		while(headerNames.hasMoreElements()){
			String nextElement = headerNames.nextElement();
			fos.write((nextElement+":"+request.getHeader(nextElement)+"\n").getBytes());
		}
		fos.write("\n".getBytes());
		ServletInputStream is = request.getInputStream();
		
		byte[] b = new byte[1024];  
		int i=0;  
		while((i=is.read(b,0,1024))!=-1){  
			fos.write(b,0,i);
		}
		fos.close();
	}
}
